package com.junlaninfo.HandwritingLock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by 辉 on 2020/8/23.
 */
public class Thread03 implements  Runnable{
    private int count = 100;
    private  AtomicTryLock atomicTryLock=new AtomicTryLock();
    private ReentrantLock lock=new ReentrantLock();

    public void run() {
        while (count > 0) {
            try {
                Thread.sleep(30);
            } catch (Exception e) {

            }

            ticket();
        }
    }

    private void ticket() {


        if (count > 0) {
            atomicTryLock.mayiktTryLock(); //获取锁
           // lock.lock();
            System.out.println(Thread.currentThread().getName() + "正在售卖：" + (100 - count + 1) + "张票");
            count--;
          //  lock.unlock();
            atomicTryLock.mayiktUnLock();//释放锁
        }


    }

    public static void main(String[] args) {
        Thread03 thread03 = new Thread03();
        Thread thread1 = new Thread(thread03, "窗口1");
        Thread thread2 = new Thread(thread03, "窗口2");
        Thread thread3 = new Thread(thread03, "窗口3");
        thread1.start();
        thread2.start();
        thread3.start();


    }
}
